package com.pz.chat.config.redis;

import com.pz.chat.config.netty.NettyConfig;
import com.pz.chat.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import java.util.concurrent.CompletableFuture;

@Slf4j
public class MyRedisConnectFactory extends LettuceConnectionFactory {
    public MyRedisConnectFactory(RedisStandaloneConfiguration configuration) {
        super(configuration);
    }

    public MyRedisConnectFactory(RedisSentinelConfiguration sentinelConfiguration) {
        super(sentinelConfiguration);
    }

    public MyRedisConnectFactory(RedisClusterConfiguration clusterConfiguration) {
        super(clusterConfiguration);
    }

    /**
     * 连接销毁前还要删除channel的注册信息
     */
    @Override
    public void destroy() {
        log.info("before redisConnect destroy...");
        NettyConfig nettyConfig = SpringUtils.getBean("nettyConfig");
        CompletableFuture.runAsync(()-> {
            if(nettyConfig != null) {
                nettyConfig.cleanRemoteChannelRegister();
            } else {
                log.error("获取 nettyConfig 失败!");
            }
        }).thenRun(super::destroy);
    }
}
